/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace moose.irc
{
	/// <summary>
	/// An IRC Message 
	/// </summary>
	[CLSCompliant(true)]
	public class IrcMessage
	{

		/// <summary>
        /// Initialize new IRC Message
        /// </summary>
        /// <param name="message">Raw Message</param>
        public IrcMessage(string message)
            : base()
        {
            m_original_message = message;
            parse(message);
            return;
        }

        /// <summary>
        /// The original message from the server
        /// </summary>
        protected string m_original_message;

        /// <summary>
        /// Origin of the message
        /// </summary>
        protected string m_source; 

        /// <summary>
        /// Command embedded in message
        /// </summary>
        protected string m_command;

        /// <summary>
        /// Collection of arguments
        /// </summary>
        protected string[] m_arguments;

        /// <summary>
        /// Returns the original message
        /// </summary>
        public string OriginalMessage
        {
            get
            {
                return m_original_message;
            }
        }

        /// <summary>
        /// Gets Message Source
        /// </summary>
        public string Source
        {
            get
            {
                return m_source;
            }
        }

        /// <summary>
        /// Gets Message Command
        /// </summary>
        public string Command
        {
            get
            {
                return m_command;
            }
        }

        /// <summary>
        /// Gets Message Arguments
        /// </summary>
        public string[] Arguments
        {
            get
            {
                return m_arguments;
            }
        }

        /// <summary>
        /// Parse Messaeg Contents
        /// </summary>
        /// <param name="message">raw message to parse</param>
        /// <remarks>
        /// there is still some work to do here...
        /// </remarks>
        protected virtual void parse(string message)
        {
            List<string> args = new List<string>();
            string trailing = null;
            if (message[0] == ':')
            {
                m_source = message.Substring(1, message.IndexOf(' '));
                message = message.Substring(message.IndexOf(' ')).Trim();
            }
            if (message.Contains(" :"))
            {
                trailing = message.Substring(message.IndexOf(" :") + 2);
                message = message.Substring(0, message.IndexOf(" :")).Trim();
            }
            args.AddRange(message.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
            m_command = args[0];
            args.RemoveAt(0);
            if (trailing != null)
                args.Add(trailing);

            m_arguments = args.ToArray(); //Convert this to an array first time, so we dont need to everywhere else...

            return;
        }

        /// <summary>
        /// Print Command out as a string
        /// </summary>
        /// <returns>string containing Command + " " + all arguments</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Command);
            sb.Append(' ');
            foreach (string arg in Arguments)
            {
                sb.Append(arg);
                sb.Append(' ');
            }
            return sb.ToString().Trim();
        }
	}
}
